﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using BMS.Models;
using BMS.Models.DataAccess;
using BMS.ViewModels;
using BMS.Utilities;

namespace BMS.Controllers
{
    public class OrderController : Controller
    {
        private OrderModelList oMOdelList = new OrderModelList();
        private FMRepository fmRepository = new FMRepository();
        private OrderRepository orderRepository = new OrderRepository();
        private GeneralRepository generalRepository = new GeneralRepository();
        //
        // GET: /Order/

        public ActionResult OrderCreation()
        {
            ViewBag.Message = "Ad-hoc / Order";
            oMOdelList.fmList = fmRepository.GetFMsForDropDownList(fmRepository.GetAllFMs().OrderBy(x => x.FMID).ToList());
            oMOdelList.emergencyList = generalRepository.GetEmergencyTypesForDropDownList(Configuration.EmergencyTypeList);
            oMOdelList.cityList = generalRepository.GetCityTypesForDropDownList(Configuration.CityTypeList);
            TempData["Order"] = null;

            return View(oMOdelList);
        }

        [HttpPost]
        public ActionResult OrderCreation(OrderModelList modelList, String FMNameSV, String ETNameSV, String CitySV)
        {
            ViewBag.Message = "Ad-hoc / Order";
            oMOdelList.fmList = fmRepository.GetFMsForDropDownList(fmRepository.GetAllFMs().OrderBy(x => x.FMID).ToList());
            oMOdelList.emergencyList = generalRepository.GetEmergencyTypesForDropDownList(Configuration.EmergencyTypeList);
            oMOdelList.cityList = generalRepository.GetCityTypesForDropDownList(Configuration.CityTypeList);
            List<OrderModel> orders = new List<OrderModel>();

            if (TempData["Order"] != null)
                oMOdelList = (OrderModelList)TempData["Order"];

            if (oMOdelList.orderList != null)
                orders = oMOdelList.orderList;

            OrderModel tempModel = new OrderModel();
            tempModel.FMName = FMNameSV;
            tempModel.ServiceOrderNo = (modelList.order.ServiceOrderNo == null) ? "" : modelList.order.ServiceOrderNo;
            tempModel.Location = (modelList.order.Location == null) ? "" : modelList.order.Location;
            tempModel.Description = (modelList.order.Description == null) ? "" : modelList.order.Description;
            tempModel.WorkOrderNo = (modelList.order.WorkOrderNo == null) ? "" : modelList.order.WorkOrderNo;
            tempModel.Emergency = ETNameSV;
            tempModel.City = CitySV;
            tempModel.OrderCreationDate = DateTime.Today;
            tempModel.IsValid = "Y";

            orders.Add(tempModel);
            oMOdelList.orderList = orders;
            ModelState.Clear();

            TempData["Order"] = oMOdelList;

            return View(oMOdelList);
        }

        public ActionResult OrderConfirmation()
        {
            ViewBag.Message = "Order Confirmation";
            List<OrderModel> orders = new List<OrderModel>();

            if (TempData["Order"] != null)
                oMOdelList = (OrderModelList)TempData["Order"];

            if (oMOdelList.orderList != null)
                orders = oMOdelList.orderList;

            bool isSuccessful = orderRepository.Insert(orders);

            OrderModelList orderMList = new OrderModelList();
            orderMList.orderList = orders;
            orderMList.InsertionSuccessfulFlag = isSuccessful;

            return View(orderMList);
        }

        public JsonResult GetOrders()
        {
            var ordersList = orderRepository.GetAllOrders();
            var orders = (from order in ordersList
                          select new
                          {
                              order.OrderCreationDate,
                              order.FMName,
                              order.ServiceOrderNo,
                              order.Location,
                              order.Description,
                              order.WorkOrderNo,
                              order.Emergency,
                              order.QuotationNo
                          });
            return Json(orders, JsonRequestBehavior.AllowGet);
        }
    }
}
